﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Xml;
namespace UcAsp.Net.BLL
{
    public class UserDefinedConfig
    {
        private static string path = AppDomain.CurrentDomain.BaseDirectory;
        private static string config = System.Configuration.ConfigurationManager.AppSettings["Config"];
        public static List<UcAsp.Net.Model.UserDefined> GetListUserDefinedConfig
        {
            get
            {
                List<UcAsp.Net.Model.UserDefined> list = new List<UcAsp.Net.Model.UserDefined>();

                XmlDocument xmlDoc = new XmlDocument();
                xmlDoc.Load(path + "\\config\\" + config + "\\UserDefined.config");
                string xmlpath = ("Root/Defined");
                if (xmlDoc.SelectNodes(xmlpath).Count != 0)
                {
                    XmlNodeList xmlList = xmlDoc.SelectNodes(xmlpath);
                    foreach (XmlNode xns in xmlList)
                    {
                        UcAsp.Net.Model.UserDefined model = new UcAsp.Net.Model.UserDefined();

                        foreach (XmlNode xn in xns)
                        {
                            XmlToObject(model, xn.Name, xn.InnerText);
                        }
                        list.Add(model);
                    }
                }
                return list;
            }
        }
        protected static void XmlToObject(object oPut, string name, string value)
        {
            System.ComponentModel.PropertyDescriptor pd = System.ComponentModel.TypeDescriptor.GetProperties(oPut).Find(name, true);
            if (pd != null)
            {
                if (pd.PropertyType == typeof(int) || pd.PropertyType == typeof(long) || pd.PropertyType == typeof(string) || pd.PropertyType == typeof(DateTime) || pd.PropertyType == typeof(bool))
                {
                    if (pd.PropertyType == typeof(int))
                    {
                        pd.SetValue(oPut, System.Convert.ToInt32(value));
                    }
                    else if (pd.PropertyType == typeof(long))
                    {
                        pd.SetValue(oPut, System.Convert.ToInt64(value));
                    }
                    else if (pd.PropertyType == typeof(double))
                    {
                        pd.SetValue(oPut, System.Convert.ToDouble(value));
                    }
                    else if (pd.PropertyType == typeof(bool))
                    {
                        pd.SetValue(oPut, System.Convert.ToBoolean(value));
                    }
                    else
                    {
                        if (value == null)
                            pd.SetValue(oPut, string.Empty);
                        else
                            pd.SetValue(oPut, value);
                    }
                }
            }
            else
            {
                //对象为空
            }
        }
        public static UcAsp.Net.Model.UserDefined GetUserDefined(string definedname)
        {
            return GetListUserDefinedConfig.FirstOrDefault(o => o.DefinedName.ToLower() == definedname.ToLower());
        }
        public static bool UpdateConfig(UcAsp.Net.Model.UserDefined model)
        {
            XmlDocument xmlDoc = new XmlDocument();
            xmlDoc.Load(path + "\\config\\" + config + "\\UserDefined.config");
            string xmlpath = ("Root/Defined");
            XmlNodeList xmlList = xmlDoc.SelectNodes(xmlpath);

            foreach (XmlNode xns in xmlList)
            {
                if (xns["DefinedName"].InnerText == model.DefinedName)
                {
                    xns["DefinedTitle"].InnerText = model.DefinedTitle;
                    xns["DefinedDescription"].InnerText = model.DefinedDescription;
                    xns["DefinedHtml"].InnerText = model.DefinedHtml;
                    xns["DefinedView"].InnerText = model.DefinedView;
                }
            }
            xmlDoc.Save(path + "\\config\\" + config + "\\UserDefined.config");
            return true;
        }
        public static bool InsertConfig(UcAsp.Net.Model.UserDefined model)
        {
            XmlDocument xmlDoc = new XmlDocument();
            xmlDoc.Load(path + "\\config\\" + config + "\\UserDefined.config");
            string xmlpath = ("Root");
            XmlNode root = xmlDoc.SelectSingleNode(xmlpath);
            XmlElement element = xmlDoc.CreateElement("Defined");

            XmlElement DefinedName = xmlDoc.CreateElement("DefinedName");
            DefinedName.InnerText = model.DefinedName;
            element.AppendChild(DefinedName);
            XmlElement DefinedTitle = xmlDoc.CreateElement("DefinedTitle");
            DefinedTitle.InnerText = model.DefinedTitle;
            element.AppendChild(DefinedTitle);
            XmlElement DefinedDescription = xmlDoc.CreateElement("DefinedDescription");
            DefinedDescription.InnerText = model.DefinedDescription;
            element.AppendChild(DefinedDescription);
            XmlElement DefinedHtml = xmlDoc.CreateElement("DefinedHtml");
            DefinedHtml.InnerText = model.DefinedHtml;
            element.AppendChild(DefinedHtml);
            XmlElement DefinedView = xmlDoc.CreateElement("DefinedView");
            DefinedView.InnerText = model.DefinedView;
            element.AppendChild(DefinedView);
            root.AppendChild(element);
            xmlDoc.Save(path + "\\config\\" + config + "\\UserDefined.config");
            return true;
        }
        public string[] DefinedView()
        {
            string path = AppDomain.CurrentDomain.BaseDirectory + "themes\\" + BLL.SettingConfig.GetSiteSetting.Theme + "\\UserDefined";
            DirectoryInfo info = new DirectoryInfo(path);
            FileInfo[] file = info.GetFiles();
            string[] dir = new string[file.Length];
            for (int i = 0; i < file.Length; i++)
            {
                dir[i] = file[i].Name.Replace(file[i].Extension, "");
            }
            return dir;
        }
    }
}